from selenium import webdriver
from time import sleep,time
from selenium.webdriver.support.select import Select
import random

from util import BoxDriver


class AddUser:

    def adduser(self,username='admin',password='123456'):
        try:
            driver = BoxDriver()

            driver.get('http://localhost/ranzhi/www/sys/user-login.html')
            driver.maximize_window()
            # 隐式等待
            driver.implicitly_wait(10)

            # 登陆
            driver.input('id,account',username)
            driver.input('id,password',password)
            driver.click('id,submit')

            # 点击后台管理
            driver.click('id,s-menu-superadmin')

            driver.switch_to_frame('id,iframe-superadmin')

            for i in range(25,30):
                # 点击 添加成员
                driver.click('p,添加成员')

                # 用户名
                driver.input('id,account','user%d'%i)

                # 真实姓名
                driver.input('id,realname','user%d'%i)

                # 性别
                driver.click('id,genderm' if i%2==0  else 'id,genderf')

                # 部门
                driver.select_by_index('id,dept',random.randint(1,6))
                # 角色
                driver.select_by_index('id,role',random.randint(1,16))

                # 密码
                driver.input('id,password1','123456')

                # 密码
                driver.input('id,password2','123456')

                # 邮箱
                driver.input('id,email','user%d@163.com'%i)

                # 保存
                driver.click('id,submit')
                # 针对 NoSuchWindow问题
                sleep(2)

                # 跳转到最后一页
                driver.input('id,_pageID','10000')
                driver.click('id,goto')
                sleep(2)

                # 获取最后一行的真实姓名
                realname = driver.locate_elements('x,/html/body/div/div/div/div[2]/div/div/table/tbody/tr/td[2]')[-1].text
                # 断言
                assert realname == 'user%d'%i,'添加用户用例失败！'

        except Exception as e:
            print(e)
        finally:
            driver.close()

if __name__ == "__main__":
    add = AddUser()
    add.adduser()